<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <input type="button" value="浪起来" @click="lang">
    <input type="button" value="停下来" @click="stop">
    <h2>{{msg}}</h2>
  </div>
</body>
<script src="../js/vue.js"></script>
<script>
  "use strict";
  const vm = new Vue({
    el: "#app",
    data: {
      msg: "我拿到了一个好的数字了",
      setId: null
    },
    methods: {
      lang() {
        //如果定时器不等于null,表示已经有定时器在执行了，此时直接退出不会再开启第二定时器了
        if (this.setId != null) return;

        this.setId = window.setInterval(() => {
          let header = this.msg.substring(0, 1);
          let end = this.msg.substring(1);
          this.msg = end + header;
        }, 300);
      },
      stop() {
        window.clearInterval(this.setId); //清除定时器
        this.setId = null;
      }
    }
  });
</script>

</html>